<?
if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true) die();

if(!isset($arParams["CACHE_TIME"]))
	$arParams["CACHE_TIME"] = 3600;

$arParams["IBLOCK_TYPE"] = trim($arParams["IBLOCK_TYPE"]);
if(strlen($arParams["IBLOCK_TYPE"])<=0)
 	$arParams["IBLOCK_TYPE"] = "lectures";
if($arParams["IBLOCK_TYPE"]=="-")
	$arParams["IBLOCK_TYPE"] = "";

if ($_SERVER["REQUEST_METHOD"]=="POST")
{
    if ($_REQUEST["FORMTYPE"]=="signup") // запись на лекцию
    {       
        $uid = $USER->GetID();
        if ($uid != $_REQUEST["LECTURE_ID"])
        {
            $el = new CIBlockElement;
            $PROP = array();
            $PROP["LECTURE"] = $_REQUEST["LECTURE_ID"];
            $PROP["TYPE"] = $type_enum_id;
            $arLoadArray = Array(
                "MODIFIED_BY"       => $uid,
                "IBLOCK_SECTION"    => false,
                "PROPERTY_VALUES"   => $PROP,
                "IBLOCK_ID"         => $arParams["IBLOCK_LOGS_ID"],
                "NAME"              => $_REQUEST["LECTURE_ID"]." - ".$uid,
                "ACTIVE"            => "Y",
            );
            $el->Add($arLoadArray);        
        }
    }
    elseif ($_REQUEST["FORMTYPE"]=="reject") // отказаться от лекции
    {
        CIBlockElement::Delete($_REQUEST["RECORD_ID"]);
    }
}

if($this->StartResultCache(false, array($USER->GetID())))
{
    if(!CModule::IncludeModule("iblock"))
    {
        $this->AbortResultCache();
        ShowError("Модуль Информационных блоков не установлен");
        return;
    }
    
//    if (!$USER->IsAuthorized())
//    {
//        $this->AbortResultCache();
//        ShowError("Вы должны авторизоваться, чтобы иметь возможность создавать заявки.");
//        return;        
//    }
    
    $arResult = array();

    $month = array(
        "01" => GetMessage("CAL_JAN"),
        "02" => GetMessage("CAL_FEB"),
        "03" => GetMessage("CAL_MAR"),
        "04" => GetMessage("CAL_APR"),
        "05" => GetMessage("CAL_MAY"),
        "06" => GetMessage("CAL_JUN"),
        "07" => GetMessage("CAL_JUL"),
        "08" => GetMessage("CAL_AUG"),
        "09" => GetMessage("CAL_SEP"),
        "10" => GetMessage("CAL_OCT"),
        "11" => GetMessage("CAL_NOV"),
        "12" => GetMessage("CAL_DEC"),
    );
    
    // *************************************************************************
    // выбираем список активных лекций
    // *************************************************************************
    $arSelect = array(
        "ID",
        "ACTIVE_FROM",
        "ACTIVE_TO",
        "NAME",
        "PREVIEW_TEXT",
        "PROPERTY_LECTOR",
        "PROPERTY_PLACE"
    );

    $arFilter = array (
        "IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
        "IBLOCK_ID"=> $arParams["IBLOCK_LECTURES_ID"],
        "ACTIVE" => "Y",
        "<=ACTIVE_FROM" => date("d.m.Y H:i:s")
    );

    $arOrder = array(
            "ACTIVE_TO"=>"ASC",
    );

    $rsItems = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);

    while ($item = $rsItems->GetNext())
    {
        if ($item["PROPERTY_LECTOR_VALUE"] == $USER->GetID())
            $item["CAN_SIGNUP"] = 0;
        elseif ($USER->IsAuthorized())
            $item["CAN_SIGNUP"] = 1;

        $item["CAN_REJECT"] = 0;
        
        $rsUser = CUser::GetByID($item["PROPERTY_LECTOR_VALUE"]);
        if ($arUser = $rsUser->Fetch())
        {
            $item["LECTOR"] = $arUser["NAME"]." ".$arUser["LAST_NAME"]; 
            $item["LECTOR_FOTO"] = $arUser["PERSONAL_PHOTO"];
            $item["LECTOR_POSITION"] = strtolower($arUser["WORK_POSITION"]);
            $res = CIBlockSection::GetByID($arUser["UF_DEPARTMENT"][0]);
            if($ar_res = $res->GetNext())
            {
                list($a, $b) = explode(" ", $ar_res["NAME"], 2);
                $item["LECTOR_DEPARTMENT"] = $b;
            }
        }
//        echo "<pre>";
//        print_r($arUser);
//        echo "</pre>";
        
        // выбор уже записавшихся
        $arSelect = array(
            "ID",
            "DATE_CREATE",
            "CREATED_BY",
        );

        $arFilter = array (
            "IBLOCK_TYPE" => $arParams["IBLOCK_TYPE"],
            "IBLOCK_ID"=> $arParams["IBLOCK_LOGS_ID"],
            "ACTIVE" => "Y",
            "PROPERTY_LECTURE" => $item["ID"]
        );

        $arOrder = array(
                "DATE_CREATE"=>"ASC",
        );

        $rsItems2 = CIBlockElement::GetList($arOrder, $arFilter, false, false, $arSelect);
        while ($item2 = $rsItems2->GetNext())
        {
            if ($item2["CREATED_BY"] == $USER->GetID())
            {
                $item["CAN_SIGNUP"] = 0;
                $item["CAN_REJECT"] = $item2["ID"];
            }
            
            $rsUser = CUser::GetByID($item2["CREATED_BY"]);
            if ($arUser = $rsUser->Fetch())
                $item2["LISTENER"] = $arUser["NAME"]." ".$arUser["LAST_NAME"]; 
            $item["AUDIENCE"][] = $item2;
        }                      
        
        list($date, $time) = explode(" ", ConvertDateTime($item["ACTIVE_FROM"],"DD.MM.YYYY HH:MI:SS"));
        list($d, $m, $y) = explode(".", $date);
        list($h, $min, $s) = explode(":", $time);
        $item["DATE"] = intval($d)." ".$month[$m]." ".intval($y);
        $item["TIME"] = $h.":".$min;
        
        $arResult[] = $item;
    }
    // *************************************************************************
    // *************************************************************************


    $this->IncludeComponentTemplate();
}
?>
